<template>
  <div>
    <el-button @click="hanldClickExport">导出</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  methods: {
    downFile(data, fileName) {
      if (!data) {
        return;
      }
      if (typeof data === "string" && data.startsWith("http")) {
        // 判断是否是链接
        const iframe = document.createElement("iframe");
        iframe.style.display = "none";
        iframe.src = data;
        document.body.appendChild(iframe);
        setTimeout(() => {
          document.body.removeChild(iframe);
        }, 1000); // 等待 1 秒后销毁 iframe
      } else {
        const url = window.URL.createObjectURL(new Blob([data]));
        const a = document.createElement("a");
        a.style.display = "none";
        a.href = url;
        a.setAttribute("download", fileName);
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);
      }
    },
    // 导出
    hanldClickExport() {
      const params = {
        data: {
          organization: this.dataLabel,
        },
      };
      // 网站邮件导出
      dzgwSafe(params)
        .then((result) => {
          this.downFile(result.resultdata, "管理制度备案表"); // 调用downFile方法进行文件下载
        })
        .catch((error) => {
          console.log(error);
        });
    },
    async exposeResult() {
      const params = {
        userId: store.getters.user_id.toString(),
        token: store.getters.token.toString(),
        system: "officedo",
        data: {
          type: "2", // 1为机构，2为人员
        },
      };
      let myDate = new Date();
      let currentTime =
        "（" +
        myDate.getFullYear() +
        "" +
        (myDate.getMonth() + 1) +
        "" +
        myDate.getDate() +
        "）";
      exposeExcel(params)
        .then((response) => {
          const blob = new Blob([response], {
            type: "application/vnd.ms-excel",
          }); // 把得到的结果用流对象转一下
          var a = document.createElement("a"); //创建一个<a></a>标签
          a.href = URL.createObjectURL(blob); // 将流文件写入a标签的href属性值
          a.download = "人员校核结果" + currentTime + ".xls"; //设置文件名
          a.style.display = "none"; // 障眼法藏起来a标签
          document.body.appendChild(a); // 将a标签追加到文档对象中
          a.click(); // 模拟点击了a标签，会触发a标签的href的读取，浏览器就会自动下载了
          a.remove(); // 一次性的，用完就删除a标签
        })
        .catch(() => {
          this.$message.error("内部错误！");
        });
    },
  },
};
</script>

<style>
</style>